package couk.mmtdigital.sm.view
{
	import couk.mmtdigital.sm.ApplicationFacade;
	import couk.mmtdigital.sm.model.FileListProxy;
	import couk.mmtdigital.sm.view.components.MainView;
	
	import mx.events.ListEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;

	public class MainViewMediator extends Mediator implements IMediator
	{
		public static const NAME:String = "MainViewMediator";
		
		private var fileRO:RemoteObject;
		private var currentRow:Number;
		private var selectedRow:Object;
		
		public function MainViewMediator(vc:Object=null)
		{
			super(NAME, vc);
			mainView.dg.addEventListener(ListEvent.ITEM_CLICK, onFileItemClick);
			mainView.dg.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, onFileItemDoubleClick);
		}
		
		private function onFileItemClick(evt:ListEvent):void{
			if(currentRow != evt.rowIndex){
				currentRow = evt.rowIndex;
				selectedRow = evt.currentTarget.selectedItem;
				trace("onFileItemClick filename: "+selectedRow.type);
				sendNotification(ApplicationFacade.ONSELECTEDFILE, selectedRow);		
			}
			if(mainView.filePn.currentState != "filePanel") mainView.filePn.currentState = "filePanel";
		}
		
		private function onFileItemDoubleClick(evt:ListEvent):void{
			var selectedRow:Object = evt.currentTarget.selectedItem;
			var fileListProxy:FileListProxy = facade.retrieveProxy(FileListProxy.NAME) as FileListProxy;
			if(selectedRow['type'] == 2){
				trace("selected item is a directroy");
				fileListProxy.currentFile += "/"+selectedRow['name'];
				sendNotification(ApplicationFacade.FILELIST);
				currentRow = -1;				
			}else if(selectedRow['type'] == 1){
				if(selectedRow['name'] == '..'){
					fileListProxy.currentFile = getParentFolder(fileListProxy.currentFile);
					trace(fileListProxy.currentFile);
					sendNotification(ApplicationFacade.FILELIST);
				}
			}
		}
		
		private function getParentFolder(s:String):String{
			var splitString:Array = s.split("/");
			var returnString:String;
			if(splitString.length > 1){
				returnString = splitString[splitString.length - 2];
			}else{
				returnString = splitString[0];
			}
			return returnString;
		}
		
		override public function listNotificationInterests():Array{
			return [
				ApplicationFacade.UPDATELIST,
				ApplicationFacade.ONSELECTEDFILE
			];
		}
		
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case ApplicationFacade.UPDATELIST:
					var fileListProxy:FileListProxy = facade.retrieveProxy(FileListProxy.NAME) as FileListProxy;
					mainView.dg.dataProvider = fileListProxy.fileList;
					break;
				case ApplicationFacade.ONSELECTEDFILE:
					var data:Object = notification.getBody();
					trace(data.name);
					mainView.filePn.fileNameLbl.text = data.name;
					mainView.filePn.fileSizeLbl.text = data.size;
					break;
			}
		}
		
		private function get mainView():MainView{
			return viewComponent as MainView;
		}
	}
}